<?php
include_once("core/session.php");

/**
 * Request
 * 
 * Class describing an HTTP Request
 * @copyright inovia
 * @package oolime_core   
 */
class Request {
    
  /**
   * Session
   *
   * @var Session
   */
  public $session;
  
  /**
   * EventDispatcher's Table of possible keys
   *
   * @var array
   */
  private $kTD = array("action","user","password");
  
  /**
   * Debugmode's Table of possible keys
   *
   * @var array
   */
  private $kTDebug = array("exportXML","user","password");
  
  
  /**
   * EventDispatcher's Table of key/values
   *
   * @var array
   */
  private $kVTD = array();
  
  /**
   * Controller's table of key/values
   *
   * @var array
   */
  private $kVTC = array();
      
  public  $database;
  private $response;
  private $logs;
    
  /**
   * Constructor parsing GET & POST values, sanitizing data at the same time, and setting the lang of the user.
   * 
   */
  public function __construct(){
    global $database;
    global $session;
    global $response;
    global $logs;
    $this->database = &$database;
    $this->session  = &$session;
    $this->response = &$response;
    $this->logs     = &$logs;
      
      /* Parsing REQUEST informations */
      foreach ($_REQUEST as $key => $value) {    
          /*SQl injection protection */
          $key    = $this->secureString($key);
          $value  = $this->secureString($value);
          
          /* if it is a ED key */
          if(in_array($key,$this->kTD)){
              /* If there is not already the same key */
              if(!array_key_exists($key,$this->kVTD)){
                  $this->kVTD[$key] = $value;
              }else {
                $this->logs->addWarning("There is already an entry for this Dispatcher key");
              }
          /* if it's an Controller key and if there is not already the same key */
          }else if(!array_key_exists($key,$this->kVTC)){
              $this->kVTC[$key] = $value;
          }else {
             $this->logs->addWarning("There is already an entry for this Controller key");
          }
      }
      //recup de la langue
      if($this->contains(Array("lgS")) ) {
 				$_SESSION["lang"]=$_REQUEST["lgS"];
    	}
    	//recup des valeurs de debug
    	//recup de XML save
    	if ($this->contains(Array("exportXML"))){
    	  $this->session->exportXML = ($_REQUEST["exportXML"]=='true');
    	  $this->session->debug     = true;
    	}
    	else {
    	  $this->session->exportXML = false;
    	}
    	//recup de JSON
    	if ($this->contains(Array("exportJSON"))){
    	  $this->session->exportJSON = ($_REQUEST["exportJSON"]=='true');
    	  $this->session->debug      = true;
    	}
    	else {
    	  $this->session->exportJSON = false;
    	}

  }

  /**
   * Displays ED & Controller Key/Value Tables
   *
   */
  public function display(){
      echo "<br/>Controls <br/>";
      foreach ($this->kVTC as $k => $v){
          echo "[".$k."] = ".$v."<br/>";
      }
      echo "<br/>D <br/>";
      foreach ($this->kVTD as $k => $v){
          echo "[".$k."] = ".$v."<br/>";
      }
  }
  
  
  
  /**
   ***DEFECT**  
   * Checks if the keys are in the Controller's table (defect)
   *
   * @param array $array
   * @return bool
   */
  public function exist($array){
    foreach ($array as $key) {
      if(!array_key_exists($key,$this->kVTC)) return false;
    }
    return true;
  }
  
  
  /**
   * Checks if the keys are in the Controller's table.
   *
   * @param array $array
   * @return bool
   */
  public function contains($array){
  foreach ($array as $key) {
      if(!array_key_exists($key,$this->kVTC)) return false;
    }
    return true;
  }
    
    
  /**
   * Checks if the keys are in the dispatcher's table
   *
   * @param array $array
   * @return bool
   */
  public function containsDKeys($array){
      foreach ($array as $key) {
          if(!array_key_exists($key,$this->kVTD)) return false;
      }
      return true;
  }
  
  /**
   * Returns the key/Value Table for Dispatcher
   *
   * @return array
   */
  public function getKeyValueTableD(){
      return $this->kVTD;
  }
  
  /**
   * Returns the key/Value Table for Controller
   *
   * @return array
   */
  public function getKeyValueTableC(){
      return $this->kVTC;
  }
    
    
  /**
   * Returns the key/Value Table for Controller
   *
   * @return array
   */
  public function setKeyValueTableC($tab){
      $this->kVTA=$tab;
  }
  
  /**
   * Returns the Value of the key in the EventDispatcher Array
   *
   * @param string $key
   * @return string
   */
  public function getValueD($key){
      if(!array_key_exists($key,$this->kVTD)) return false;
      return $this->kVTD[$key];
  }
  
  /**
   ***DEFECT**  
   * Returns the Value of the key in the Controller Array (defect)
   *
   * @param string $key
   * @return string
   */
  public function getValueC($key){
      if(!array_key_exists($key,$this->kVTC)) return false;
      return $this->kVTC[$key];
  }
  
  /**
   * Returns the Value of the key in the Controller Array
   *
   * @param string $key
   * @return string
   */
  public function get($key){
    if(isset($this->kVTC[$key])) return $this->kVTC[$key];
    else return null;
  }
  
  public function secureString($string){
    if(is_array($string)){
      foreach($string as $key => $value){
        addslashes($string[$key]);
      }
      return $string;
    }else{
      return addslashes($string);
    }
  }
    
    
    
    
  /**
	 * 
	 * Checks the type of an array of request values.
	 * $tab is an array of (name =>(type => error message)). The error message will be added on the response tree, with the $level error level specified.
	 *
	 * @param array table of type check and error message related 
	 */                       
	public function check($tab) {
		global $configAssocTypes;

		$flag = true;

		while(list($key,$val) = each($tab)) {
			$type = $val[0];
			if(isset($configAssocTypes[$type])) {
				$pattern = $configAssocTypes[$type];
			} 
			else {
				$pattern = $type;
			}
			$text = $val[1];


			if(!$this->contains(Array($key)) || ($value = $this->get($key))==null || eregi($pattern,$value)==null) {
				$this->response->addError($text,$key);
				$flag = false;
			}

		}

		return $flag;
	}
    
}   
?>
